package cn.ondraw.ragflow.service.impl;

import cn.hutool.json.JSONUtil;
import cn.ondraw.ragflow.pojo.request.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import cn.ondraw.ragflow.pojo.request.*;
import cn.ondraw.ragflow.pojo.request.*;
import cn.ondraw.ragflow.pojo.response.CanvasResponse;
import cn.ondraw.ragflow.service.RagFlowCanvasService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
@RequiredArgsConstructor
public class RagFlowCanvasServiceImpl implements RagFlowCanvasService {

    private final PythonApiCaller pythonApiCaller;

    private final String URL_PREFIX = "/canvas";

    /**
     * 获取模板列表
     *
     * @return 模板列表响应对象
     */
    @Override
    public CanvasResponse templates() {
        ResponseEntity<?> response = pythonApiCaller.callGetApi(URL_PREFIX + "/templates");
        return JSONUtil.toBean(response.getBody() != null ? response.getBody().toString() : null, CanvasResponse.class, true);
    }

    /**
     * 获取画布列表
     *
     * @return 画布列表响应对象
     */
    @Override
    public CanvasResponse canvasList() {
        ResponseEntity<?> response = pythonApiCaller.callGetApi(URL_PREFIX + "/list");
        return JSONUtil.toBean(response.getBody() != null ? response.getBody().toString() : null, CanvasResponse.class, true);
    }

    /**
     * 删除画布
     *
     * @param request 删除画布请求对象
     * @return 删除操作响应对象
     */
    @Override
    public CanvasResponse rm(CanvasRmRequest request) {
        ResponseEntity<?> response = pythonApiCaller.callPostApi(URL_PREFIX + "/rm", request);

        return JSONUtil.toBean(response.getBody() != null ? response.getBody().toString() : null, CanvasResponse.class, true);
    }

    /**
     * 保存画布
     *
     * @param request 保存画布请求对象
     * @return 保存操作响应对象
     */
    @Override
    public CanvasResponse save(SetRequest request) {
        ResponseEntity<?> response = pythonApiCaller.callPostApi(URL_PREFIX + "/set", request);
        return JSONUtil.toBean(response.getBody() != null ? response.getBody().toString() : null, CanvasResponse.class, true);
    }

    /**
     * 获取指定 ID 的画布
     *
     * @param canvasId 画布 ID
     * @return 画布信息响应对象
     */
    @Override
    public CanvasResponse get(String canvasId) {
        ResponseEntity<?> response = pythonApiCaller.callGetApi(URL_PREFIX + "/get/" + canvasId);
        return JSONUtil.toBean(response.getBody() != null ? response.getBody().toString() : null, CanvasResponse.class, true);
    }

    /**
     * 获取指定 ID 画布的 SSE 信息
     *
     * @param canvasId 画布 ID
     * @param token    认证令牌
     * @return SSE 信息响应对象
     */
    @Override
    public CanvasResponse getsse(String canvasId, String token) {
        // 此处需调整 PythonApiCaller 以支持请求头设置
        ResponseEntity<?> response = pythonApiCaller.callGetApi(URL_PREFIX + "/getsse/" + canvasId);
        return JSONUtil.toBean(response.getBody() != null ? response.getBody().toString() : null, CanvasResponse.class, true);
    }

    /**
     * 执行画布完成操作
     *
     * @param request 完成操作请求对象
     * @return 完成操作响应对象
     */
    @Override
    public CanvasResponse completion(CompletionRequest request) {
        ResponseEntity<?> response = pythonApiCaller.callPostApi(URL_PREFIX + "/completion", request);
        return JSONUtil.toBean(response.getBody() != null ? response.getBody().toString() : null, CanvasResponse.class, true);
    }

    /**
     * 重置画布
     *
     * @param request 重置操作请求对象
     * @return 重置操作响应对象
     */
    @Override
    public CanvasResponse reset(ResetRequest request) {
        ResponseEntity<?> response = pythonApiCaller.callPostApi(URL_PREFIX + "/reset", request);
        return JSONUtil.toBean(response.getBody() != null ? response.getBody().toString() : null, CanvasResponse.class, true);
    }

    /**
     * 获取输入元素信息
     *
     * @param request 输入元素请求对象
     * @return 输入元素信息响应对象
     */
    @Override
    public CanvasResponse inputElements(InputElementsRequest request) {
        Map<String, String> params = new HashMap<>();
        params.put("id", request.getCvs_id());
        params.put("component_id", request.getCpn_id());
        // 暂时无法在 PythonApiCaller 中设置请求参数，需后续调整 PythonApiCaller 实现
        ResponseEntity<?> response = pythonApiCaller.callGetApi(URL_PREFIX + "/input_elements");
        return JSONUtil.toBean(response.getBody() != null ? response.getBody().toString() : null, CanvasResponse.class, true);
    }

    /**
     * 调试画布
     *
     * @param request 调试请求对象
     * @return 调试操作响应对象
     */
    @Override
    public CanvasResponse debug(DebugRequest request) {
        ResponseEntity<?> response = pythonApiCaller.callPostApi(URL_PREFIX + "/debug", request);
        return JSONUtil.toBean(response.getBody() != null ? response.getBody().toString() : null, CanvasResponse.class, true);
    }

    /**
     * 测试数据库连接
     *
     * @param request 数据库连接测试请求对象
     * @return 测试结果响应对象
     */
    @Override
    public CanvasResponse testDbConnect(TestDbConnectRequest request) {
        ResponseEntity<?> response = pythonApiCaller.callPostApi(URL_PREFIX + "/test_db_connect", request);
        return JSONUtil.toBean(response.getBody() != null ? response.getBody().toString() : null, CanvasResponse.class, true);
    }

    /**
     * 获取指定画布的版本列表
     *
     * @param canvasId 画布 ID
     * @return 版本列表响应对象
     */
    @Override
    public CanvasResponse getlistversion(String canvasId) {
        ResponseEntity<?> response = pythonApiCaller.callGetApi(URL_PREFIX + "/getlistversion/" + canvasId);
        return JSONUtil.toBean(response.getBody() != null ? response.getBody().toString() : null, CanvasResponse.class, true);
    }

    /**
     * 获取指定版本信息
     *
     * @param versionId 版本 ID
     * @return 版本信息响应对象
     */
    @Override
    public CanvasResponse getversion(String versionId) {
        ResponseEntity<?> response = pythonApiCaller.callGetApi(URL_PREFIX + "/getversion/" + versionId);
        return JSONUtil.toBean(response.getBody() != null ? response.getBody().toString() : null, CanvasResponse.class, true);
    }

    /**
     * 列出知识库
     *
     * @param request 列出知识库请求对象
     * @return 知识库列表响应对象
     */
    @Override
    public CanvasResponse list_kbs(ListTeamRequest request) {
        Map<String, String> params = new HashMap<>();
        params.put("keywords", request.getKeywords());
        params.put("page", String.valueOf(request.getPage_number()));
        params.put("page_size", String.valueOf(request.getItems_per_page()));
        params.put("orderby", request.getOrderby());
        params.put("desc", String.valueOf(request.isDesc()));
        // 暂时无法在 PythonApiCaller 中设置请求参数，需后续调整 PythonApiCaller 实现
        ResponseEntity<?> response = pythonApiCaller.callGetApi(URL_PREFIX + "/listteam");
        return JSONUtil.toBean(response.getBody() != null ? response.getBody().toString() : null, CanvasResponse.class, true);
    }

    /**
     * 设置画布信息
     *
     * @param request 设置请求对象
     * @return 设置操作响应对象
     */
    @Override
    public CanvasResponse setting(SettingRequest request) {
        ResponseEntity<?> response = pythonApiCaller.callPostApi(URL_PREFIX + "/setting", request);
        return JSONUtil.toBean(response.getBody() != null ? response.getBody().toString() : null, CanvasResponse.class, true);
    }

}
